var PrintTemplateUtil = {
  getSumStat: function (tableData, field, decimal) {
    var sum = _.sumBy(tableData, function (data) {
      var value = _.get(data, field);
      if (SysUtil.isEmpty(value)) {
        return 0;
      }
      return value;
    });
    if (decimal !== undefined) {
      if (typeof decimal === 'string') {
        if (decimal === '') {
          return sum;
        }
        decimal = parseInt(decimal);
      }
      sum = _.round(sum, decimal);
    }
    return sum;
  },

  getCapitalCurrency: function (tableData, field, funName) {
    var value;
    if (funName !== undefined) {
      value = PrintTemplateUtil[funName](tableData, field);
    } else {
      value = _.get(tableData, field, '');
    }

    return PrintTemplateUtil.convertCurrency(value);
  },

  convertCurrency: function (currencyDigits) {
    if (currencyDigits === "") {
      return "";
    }

    var MAXIMUM_NUMBER = 99999999999.99;
    if (currencyDigits > MAXIMUM_NUMBER) {
      return "";
    }

    var integral;
    var decimal;
    currencyDigits = currencyDigits.toString();
    var parts = currencyDigits.split(".");
    if (parts.length > 1) {
      integral = parts[0];
      decimal = parts[1];
      decimal = decimal.substr(0, 2);
    } else {
      integral = parts[0];
      decimal = "";
    }

    var digits = "零壹贰叁肆伍陆柒捌玖".split("");
    var radices = ["", "拾", "佰", "仟"];
    var bigRadices = ["", "万", "亿"];
    var decimals = ["角", "分", "元"];
    var outputCharacters = "";
    if (Number(integral) > 0) {
      var zeroCount = 0;
      for (var i = 0; i < integral.length; i++) {
        var p = integral.length - i - 1;
        var d = integral.charAt(i);
        var quotient = p / 4;
        var modulus = p % 4;
        if (d === "0") {
          zeroCount++;
        } else {
          if (zeroCount > 0) {
            outputCharacters += digits[0];
          }
          zeroCount = 0;
          outputCharacters += digits[Number(d)] + radices[modulus];
        }
        if (modulus === 0 && zeroCount < 4) {
          outputCharacters += bigRadices[quotient];
        }
      }
      outputCharacters += decimals[2];
    }
    if (decimal !== "") {
      for (var i = 0; i < decimal.length; i++) {
        var d = decimal.charAt(i);
        if (d === '0') {
          outputCharacters += digits[Number(d)];
        } else {
          outputCharacters += digits[Number(d)] + decimals[i];
        }
      }
    }
    if (outputCharacters === "") {
      outputCharacters = digits[0] + decimals[2];
    }
    if (decimal === "") {
      outputCharacters += "整";
    }
    return outputCharacters;
  },

  arrayJoin: function (arr, separator) {
    if (separator === undefined) {
      separator = '';
    }
    return arr.join(separator);
  },

  arrayJoinParam: function () {
    var arr = Array.prototype.concat.apply([], arguments);
    var separator = arr.pop();
    return arr.join(separator);
  },

  getMergeNumber: function (field, tableData, rowIndex, config) {
    var data = tableData[rowIndex];
    var mergeRulName = config.mergeRulName;
    if (mergeRulName === undefined) {
      return data[field];
    }

    var megerRul = data.megerRul;
    if (megerRul === undefined) {
      return data[field];
    }

    var rul = megerRul[mergeRulName];
    if (rul === undefined || rul <= 1) {
      return data[field];
    }

    var datas = tableData.slice(rowIndex, rowIndex + rul);
    var sum = _.sumBy(datas, field);
    return sum;
  },

  getPurchasePrefixSpan: function (orderType, num) {
    if (orderType === 1) {
      return num;
    }
    return num - 1;
  }
}